package com.hitqz.robot.driver.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.api.business.enums.FormConfigEnum;
import com.hitqz.robot.api.business.event.FormUpdateEvent;
import com.hitqz.robot.api.common.dto.form.FormFieldConfigUpdateDto;
import com.hitqz.robot.api.common.dto.warning.AddWarningDto;
import com.hitqz.robot.driver.communication.NetworkServiceThreadPool;
import com.hitqz.robot.driver.dto.GfRobotStatus;
import com.hitqz.robot.driver.util.GfToolkit;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
@AllArgsConstructor
@Slf4j
public class GfSelfListener {

    private final RateLimiter limiter = RateLimiter.create(1);

    @EventListener
    public void onLocation(GfRobotStatus event) {
        NetworkServiceThreadPool.dealEvent(() -> {
            short power = event.getPower();
            if (power<=20 && limiter.tryAcquire(60*2)){
                log.info("电量过低，关闭电机、相机");
                AddWarningDto addWarningDto = new AddWarningDto();
                addWarningDto.setTitle("低电量告警");
                addWarningDto.setCode("");
                addWarningDto.setSuggestion("请确保合适电量后手动开启使能");
                addWarningDto.setLevel(0);
                if (event.getGsDriverEnable()==(byte)1){
                    GfToolkit.setIoEnable((byte)0x03,(byte)0x02);
                    addWarningDto.setContent("自动关闭滚刷电机使能");
                    SpringUtil.getApplicationContext().publishEvent(addWarningDto);
                }
                if (event.getRunningDriverEnable()==(byte)1){
                    GfToolkit.setIoEnable((byte)0x02,(byte)0x02);
                    addWarningDto.setContent("自动关闭驱动电机使能");
                    SpringUtil.getApplicationContext().publishEvent(addWarningDto);

                }
                if (event.getCameraAndWiperEnable()==(byte)1){
                    GfToolkit.setIoEnable((byte)0x01,(byte)0x02);
                    addWarningDto.setContent("自动关闭相机使能");
                    SpringUtil.getApplicationContext().publishEvent(addWarningDto);
                }

            }
        });
    }





}
